﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YXShop.IDAL.Accessories;
using System.Data;
using System.Data.SqlClient;

namespace YXShop.SQLServerDAL.Accessories
{
    public class Questionnaire:IQuestionnaire
    {
        #region  "DataBase Operation"
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(YXShop.Model.Accessories.Questionnaire model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [yxs_questionnaire](");
            strSql.Append("[quest_name],[quest_describe],[quest_iphand],[quest_vote],[quest_pwd],[quest_way],[quest_endtime],[quest_rowform],[quest_appearance])");
            strSql.Append("values(");
            strSql.Append("@name,@describe,@iphand,@vote,@pwd,@way,@endtime,@rowform,@appearance) Select scope_IDENTITY()");
            SqlParameter[] parameters = (SqlParameter[])this.ValueParams(model);
            object obj = ChangeHope.DataBase.SQLServerHelper.GetSingle(strSql.ToString(),parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        public void Update(YXShop.Model.Accessories.Questionnaire model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [yxs_questionnaire] set  ");
            strSql.Append("[quest_name]=@name,[quest_describe]=@describe,[quest_iphand]=@iphand,[quest_vote]=@vote,[quest_pwd]=@pwd,[quest_way]=@way,[quest_endtime]=@endtime,[quest_rowform]=@rowform,[quest_appearance]=@appearance  ");
            strSql.Append("where [quest_id]=@id");
            SqlParameter[] parameters = (SqlParameter[])this.ValueParams(model);
            ChangeHope.DataBase.SQLServerHelper.ExecuteSql(strSql.ToString(),parameters);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        public void Delete(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [yxs_questionnaire] ");
            strSql.Append("where [quest_id]=@id");
            SqlParameter[] parameters ={
                                        new SqlParameter("@id",SqlDbType.Int,4)
                                    };
            parameters[0].Value = id;
            ChangeHope.DataBase.SQLServerHelper.ExecuteSql(strSql.ToString(),parameters);
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        public void DeleteAll(string ids)
        {   
            StringBuilder strSql=new StringBuilder();
            strSql.Append("delete from [yxs_questionnaire] where [quest_id] in(");
            strSql.Append(ids+" )");
            ChangeHope.DataBase.SQLServerHelper.ExecuteSql(strSql.ToString());
        }
        #endregion

        #region "Data Load"
        /// <summary>
        /// 根据ID查对象
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public YXShop.Model.Accessories.Questionnaire GetModelById(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 [quest_id],[quest_name],[quest_describe],[quest_iphand],[quest_vote],[quest_pwd],[quest_way],[quest_endtime],[quest_rowform],[quest_appearance] from [yxs_questionnaire] ");
            strSql.Append("where [quest_id]=@id");
            SqlParameter[] parameters ={
                                        new SqlParameter("@id",SqlDbType.Int,4)
                                    };
            parameters[0].Value = id;
            YXShop.Model.Accessories.Questionnaire model = null;
            using(SqlDataReader reader=ChangeHope.DataBase.SQLServerHelper.ExecuteReader(strSql.ToString(),parameters)){
                while(reader.Read()){
                    model = new YXShop.Model.Accessories.Questionnaire();
                    model.Questid = (int)reader["quest_id"];
                    model.Questname = (string)reader["quest_name"];
                    model.Questdescribe = (string)reader["quest_describe"];
                    model.Questiphand = Convert.ToInt32(reader["quest_iphand"]);
                    model.Questvote = Convert.ToInt32(reader["quest_vote"]);
                    model.Questpwd = (string)reader["quest_pwd"];
                    model.Questway = Convert.ToInt32(reader["quest_way"]);
                    model.Questendtime = Convert.ToDateTime(reader["quest_endtime"]);
                    model.Questrowform = (string)reader["quest_rowform"];
                    model.Questappearance =Convert.ToInt32(reader["quest_appearance"]);
                }
            }
            return model;
        }
        /// <summary>
        /// 得到指定条件列表
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public List<YXShop.Model.Accessories.Questionnaire> GetAll(string strWhere)
        {
            List<YXShop.Model.Accessories.Questionnaire> list = new List<YXShop.Model.Accessories.Questionnaire>();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 [quest_id],[quest_name],[quest_describe],[quest_iphand],[quest_vote],[quest_pwd],[quest_way],[quest_endtime],[quest_rowform],[quest_appearance] from [yxs_questionnaire]   ");
            if (strWhere != null && strWhere != "")
            {
                strSql.Append("where " + strWhere + " ");
            }
            using (SqlDataReader reader = ChangeHope.DataBase.SQLServerHelper.ExecuteReader(strSql.ToString()))
            {
                while (reader.Read())
                {
                    YXShop.Model.Accessories.Questionnaire model = new YXShop.Model.Accessories.Questionnaire();
                    model.Questid = (int)reader["quest_id"];
                    model.Questname = (string)reader["quest_name"];
                    model.Questdescribe = (string)reader["quest_describe"];
                    model.Questiphand = Convert.ToInt32(reader["quest_iphand"]);
                    model.Questvote = Convert.ToInt32(reader["quest_vote"]);
                    model.Questpwd = reader["quest_pwd"].ToString();
                    model.Questway = Convert.ToInt32(reader["quest_way"]);
                    model.Questendtime = Convert.ToDateTime(reader["quest_endtime"]);
                    model.Questrowform = (string)reader["quest_rowform"];
                    model.Questappearance = Convert.ToInt32(reader["quest_appearance"]);
                    list.Add(model);
                }
            }
            return list;
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <returns></returns>
        public ChangeHope.DataBase.DataByPage GetAllList()
        {
            string sql = "[select] * [from] yxs_questionnaire [where] 1=1 [order by] quest_id asc";
            ChangeHope.DataBase.DataByPage datapage = new ChangeHope.DataBase.DataByPage();
            datapage.Sql = sql;
            datapage.GetRecordSetByPage();
            return datapage;
        }

        #endregion

        #region "Other function"
        public IDataParameter[] ValueParams(YXShop.Model.Accessories.Questionnaire model)
        {
            SqlParameter[] parameters=new SqlParameter[10];
            parameters[0] = new SqlParameter("@id",SqlDbType.Int,4);
            parameters[1] = new SqlParameter("@name",SqlDbType.VarChar,50);
            parameters[2] = new SqlParameter("@describe",SqlDbType.VarChar,1000);
            parameters[3] = new SqlParameter("@iphand",SqlDbType.Int,4);
            parameters[4] = new SqlParameter("@vote",SqlDbType.Int,4);
            parameters[5] = new SqlParameter("@pwd",SqlDbType.VarChar,50);
            parameters[6] = new SqlParameter("@way",SqlDbType.Int,4);
            parameters[7] = new SqlParameter("@endtime",SqlDbType.DateTime,8);
            parameters[8] = new SqlParameter("@rowform",SqlDbType.VarChar,2000);
            parameters[9] = new SqlParameter("@appearance",SqlDbType.Int,4);

            parameters[0].Value = model.Questid;
            parameters[1].Value = model.Questname;
            parameters[2].Value = model.Questdescribe;
            parameters[3].Value = model.Questiphand;
            parameters[4].Value = model.Questvote;
            parameters[5].Value = model.Questpwd;
            parameters[6].Value = model.Questway;
            parameters[7].Value = model.Questendtime;
            parameters[8].Value = model.Questrowform;
            parameters[9].Value = model.Questappearance;
            return parameters;
        }
        #endregion
    }
}
